package cn.neusoft.yuejian307.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.neusoft.yuejian307.R;
import cn.neusoft.yuejian307.bean.User;
import cn.neusoft.yuejian307.listener.UserListener;
import cn.neusoft.yuejian307.model.UserModel;
import cn.neusoft.yuejian307.tencent.QQTencent;

import static cn.neusoft.yuejian307.GloableVirable.USERID;


public class LoginActivity extends AppCompatActivity implements UserListener<User>{

    private TextView register;
    private Button commit;
    private EditText phone,password;
    private String str1,str2;
    private ImageView qq;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
    }
    private void init(){
        register = (TextView)findViewById(R.id.register);
        commit = (Button)findViewById(R.id.commit);
        phone = (EditText)findViewById(R.id.phone);
        password = (EditText)findViewById(R.id.password);
        qq = (ImageView)findViewById(R.id.qq);


        commit.setOnClickListener(new View.OnClickListener() {  //登录
            @Override
            public void onClick(View view) {
                mLogin();
            }
        });
        register.setOnClickListener(new View.OnClickListener() {//注册
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,Register.class);
                startActivityForResult(intent,1);
            }
        });
        qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new QQTencent(LoginActivity.this).qqLogin();
            }
        });

    }

    private void mLogin(){
        str1 = phone.getText().toString();
        str2 = password.getText().toString();

        if (str1.equals("")||str2.equals("")) {
            display("你麻痹的，让你输密码后再登录。什么都不写，你是不是傻？二逼。。。。。");
        }
        else{
            UserModel userModel = new UserModel();
            userModel.loginData(this,str1,str2);
        }

    }



    private void display(String str){
        Toast.makeText(this,str,Toast.LENGTH_LONG).show();
    }

    @Override
    public void onResponse(User user) {
        if (!user.getUid().equals("0")){
            display("登录成功");
            USERID = Integer.parseInt(user.getUid());
            Intent intent = new Intent(this,MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onFail(String msg) {
        display("链接服务器失败");
    }

}
